﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ComponentModel.Composition;

namespace Kooboo.Commerce.Repositories
{
    [Export(typeof(IProduct))]
    public partial class Product : Entity, IProduct
    {
        #region IProduct Members

        PackageSize _PackageSize;
        public PackageSize PackageSize
        {
            get
            {
                if (this._PackageSize == null)
                {
                    this._PackageSize = new PackageSize
                    {
                        Height = this.PackageSize_Height,
                        Length = this.PackageSize_Length,
                        Width = this.PackageSize_Width
                    };
                }

                return this._PackageSize;
            }
            set
            {
                this._PackageSize = value;
                if (value == null)
                {
                    this.PackageSize_Height = null;
                    this.PackageSize_Length = null;
                    this.PackageSize_Width = null;
                }
                else
                {
                    this.PackageSize_Height = value.Height;
                    this.PackageSize_Length = value.Length;
                    this.PackageSize_Width = value.Width;
                }
            }
        }

        IBrand _brand;
        IBrand IProduct.Brand
        {
            get
            {
                if (this._brand == null)
                {
                    this._brand = this.DataContext.Brands
                        .Where(i => i.Id == this.BrandId)
                        .FirstOrDefault();
                }

                return this._brand;
            }
            set
            {
                this._brand = value;

                if (value == null)
                {
                    this.BrandId = null;
                }
                else
                {
                    this.BrandId = value.Id;
                }
            }
        }

        IProductType _productType;
        IProductType IProduct.ProductType
        {
            get
            {
                if (!ProductTypeId.HasValue)
                {
                    return null;
                }

                if (_productType == null)
                {
                    _productType = DataContext.ProductTypes.Where(p => p.Id == ProductTypeId.Value).FirstOrDefault();
                }
                return _productType;
            }
            set
            {
                _productType = value;
                if (_productType != null)
                {
                    ProductTypeId = _productType.Id;
                }
            }
        }

        ITax _tax;
        ITax IProduct.Tax
        {
            get
            {
                if (!TaxId.HasValue)
                {
                    return null;
                }

                if (_tax == null)
                {
                    _tax = DataContext.Taxes.Where(t => t.Id == TaxId.Value).FirstOrDefault();
                }
                return _tax;
            }
            set
            {
                _tax = value;
                if (_tax != null)
                {
                    TaxId = _tax.Id;
                }
            }
        }

        List<ISaleItem> _saleItems;
        List<ISaleItem> IProduct.SaleItems
        {
            get
            {
                if (_saleItems == null)
                {
                    _saleItems = DataContext.SaleItems.Where(s => s.ProductId == Id).Select(s => s as ISaleItem).ToList();
                }
                return _saleItems;
            }
            set
            {
                _saleItems = value;
            }
        }

        List<IProductImage> _productImages;
        List<IProductImage> IProduct.ProductImages
        {
            get
            {
                if (_productImages == null)
                {
                    _productImages = DataContext.ProductImages.Where(i => i.ProductId == Id).Select(i => i as IProductImage).ToList();
                }
                return _productImages;
            }
            set
            {
                _productImages = value;
            }
        }

        List<IProductCustomField> _productCustomFields;
        List<IProductCustomField> IProduct.ProductCustomFields
        {
            get
            {
                if (_productCustomFields == null)
                {
                    _productCustomFields = DataContext.ProductCustomFields.Where(p => p.ProductId == Id).Select(p => p as IProductCustomField).ToList();
                }
                return _productCustomFields;
            }
            set
            {
                _productCustomFields = value;
            }
        }

        IQueryable<ICategory> _categories;
        public IQueryable<ICategory> Categories
        {
            get
            {
                if (_categories == null)
                {
                    _categories = DataContext.CategoryProducts.Where(c => c.ProductId == Id).Select(c => c.Category as ICategory);
                }
                return _categories;
            }
            set
            {
                _categories = value;
            }
        }

        IQueryable<IProductReview> _productReviews;
        IQueryable<IProductReview> IProduct.ProductReviews
        {
            get
            {
                if (_productReviews == null)
                {
                    _productReviews = DataContext.ProductReviews.Where(p => p.ProductId == Id).Select(p => p as IProductReview);
                }
                return _productReviews;
            }
        }

        IQueryable<IProductQuestion> _productQuestions;
        IQueryable<IProductQuestion> IProduct.ProductQuestions
        {
            get
            {
                if (_productQuestions == null)
                {
                    _productQuestions = DataContext.ProductQuestions.Where(p => p.ProductId == Id).Select(p => p as IProductQuestion);
                }
                return _productQuestions;
            }
        }

        #endregion
    }
}
